package net.easyjava.third.juhe.client;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.juhe.bean.LineBean;
import net.easyjava.third.juhe.bean.ResultBean;
import net.easyjava.third.juhe.utils.ApiUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wxnacy on 16/7/17.
 */
public class TrafficClient {
//    private String key;
    private static TrafficClient singleton;
    private TrafficClient(){
//        this.key = key;
    }
    public static TrafficClient getInstance() {
        if (singleton == null) {
            synchronized (TrafficClient.class) {
                if (singleton == null) {
                    singleton = new TrafficClient();
                }
            }
        }
        return singleton;
    }

    public static final String URL_BUS_LINE = "http://op.juhe.cn/189/bus/busline";


    public static final String DTYPE = "json";


    /**
     * 获取公交列表
     * @param city
     * @param bus
     * @return
     * @throws IOException
     */
    public List<LineBean> getBusLine(String key,String city,String bus) throws IOException {
        Map<String,String> params = new HashMap<>();
        params.put("key",key);
        params.put("dtype",DTYPE);
        params.put("city",city);
        params.put("bus",bus);
        ResultBean resultBean = ApiUtils.send(URL_BUS_LINE, params);

        List<LineBean> list = JSONObject.parseArray(resultBean.getResult(),LineBean.class);
        return list;
    }


    public static void main(String[] args) {
        TrafficClient trafficClient = TrafficClient.getInstance();
        try {
//            System.out.println("trafficClient.getBusLine(\"北京\",\"455\") = " + trafficClient.getBusLine("0376b00558309f4589a5256562e9b788","北京", "455"));
            List<LineBean> list = trafficClient.getBusLine("0376b00558309f4589a5256562e9b788","北京", "455");
            String keyName = list.get(0).getKeyName();
            String name = "455";
            System.out.println("name.equalsIgnoreCase(keyName) = " + name.equalsIgnoreCase(keyName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
